/**************************************************************************
* FXSerial
* Copyright (C) 2010 thierry lorthiois (lorthiois@bbsoft.fr)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**************************************************************************/
/***************************************************************************
 * Asynchronous FTP download/upload using libcurl library.
 * 
 * load files from 'source' to 'destination' according to 'pattern'.
 * 'source' and 'destination' can be FTP server or local directory.
 * use standard notation for FTP server : ftp://login:password@server.
 *
 * TODO : 
 * - allow name of directory with space
 * 
 ***************************************************************************/
#ifndef FXFTP_H
#define FXFTP_H

#include <stdio.h>
#include <stdlib.h>
#include "fx.h"
#include "FXThread.h"
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>


class FXFTP : public FXThread {
public:
   // copy files from 'src/pattern' to 'dest'.
   // 'src' or 'dest' can be an FTP server or local directory.
   // use standard ftp notation : ftp://login:password@server
   // 'src' and 'dest' ended without  '/'  ???????????????????
   FXFTP(const FXString& src, const FXString& dest, const FXString& pattern);
   ~FXFTP();

   // list all files from 'server' according to pattern
   // 'pattern' doesn't work on sftp server
   // libcurl send EOL according to server's system ('\n' on linux, '\r\n' on windows)
   int listServerFiles(FXString*& tabFile);

   // total size and current size downloaded/uploaded
   long long tailleTotal, tailleEnCours;

   // commande post transfert. sur chaque fichier transférée.
   FXString postCommand;
   
   FXString erreur;
   
   // destination file
   FILE *stream;

   CURL *curl;
   
protected:
   virtual FXint run();

private:
   void upload();
   void download();
         
private:
   enum { DOWNLOAD, UPLOAD };
   int mode;
   
   // ftp server, login and password (ftp://login:password@server)
   FXString server;

   FXString localDirectory;
   
   FXString filePattern;
};


#endif

